package com.shujia.flink.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.io.File;
import java.time.Duration;

public class Demo02FileSource {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 历史版本读文件的方式，有界流
        DataStream<String> oldFileDS = env.readTextFile("flink/data/words.txt");
//        oldFileDS.print();

        // 新版本加载文件的方式：FileSource，默认是有界流
        FileSource<String> fileSource = FileSource
                .forRecordStreamFormat(
                        new TextLineInputFormat()
                        , new Path("flink/data/words.txt")
                )
                .build();
        // 从Source加载数据构建DS
        DataStream<String> fileSourceDS = env.fromSource(fileSource, WatermarkStrategy.noWatermarks(), "fileSource");
//        fileSourceDS.print();

        // 将读取文件变成无界流
        FileSource<String> fileSource2 = FileSource
                .forRecordStreamFormat(
                        new TextLineInputFormat()
                        , new Path("flink/data/words")
                )
                // 类似Flume中的spool dir，可以监控一个目录下文件的变化
                .monitorContinuously(Duration.ofSeconds(5))
                .build();

        DataStream<String> fileSourceDS2 = env.fromSource(fileSource2,WatermarkStrategy.noWatermarks(),"fileSource2");
        fileSourceDS2.print();
        env.execute();


    }
}
